import {AsyncStorage} from 'react-native'
import {AuthModel, OrderModel} from "../interface/index";
import Request from "../utils/Request";

const STORAGE_KEY = "STORAGE_KEY_order";
export default {
    namespace: 'order',
    state: {


        loading: false,
        //当前tab
        currentTab: 0,


        ///新订单
        newOrderList: [],
        newOrderNextPageUrl: null,


        //取消订单
        cancelOrderList: [],
        cancelOrderNextPageUrl: null,

        //完成订单
        finishOrderList: [],
        finishOrderNextPageUrl: null,

        //退款售后订单
        refundOrderList: [],
        refundOrderNextPageUrl: null,

        //进行中
        progressOrderList: [],
        progressOrderNextPageUrl: null,
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {

        //保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        //本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        //更改当前tab
        * changeCurrentTab({payload: {currentTab}}, {call, put, select}) {
            const order: OrderModel = yield select(state => state.order);
            if (order.currentTab !== currentTab) {
                yield put({
                    type: 'save',
                    payload: {currentTab}
                });
                yield put({type: 'fetchData'})
            }
        },

        //获取订单列表
        * fetchData({payload}, {call, put, select}) {
            const order: OrderModel = yield select(state => state.order);


            console.log('开始获取订单信息', order);
            switch (order.currentTab) {
                case 0: {
                    yield put({type: 'newOrderList'})
                }
                    break;
                case 1: {
                    yield put({type: 'cancelOrderList'})
                }
                    break;
                case 2: {
                    yield put({type: 'finishOrderList'})
                }
                    break;
                case 4: {
                    yield put({type: 'refundOrderList'})
                }
                    break;
                case 3: {
                    yield put({type: 'progressOrderList'})
                }
                    break;
            }
        },
        //获取新订单列表
        * newOrderList({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            console.log('开始获取新订单列表', payload);
            const data = yield call(Request.get, Request.getApi().newOrder, {store_id: auth.profile.id, ...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        newOrderNextPageUrl: data.data.next_page_url,
                        newOrderList: data.data.data
                    }
                })
            }

        },
        //取消订单列表
        * cancelOrderList({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            console.log('开始获取取消单列表', payload);
            const data = yield call(Request.get, Request.getApi().cancelOrder, {store_id: auth.profile.id, ...payload})
            yield put({
                type: 'save',
                payload: {loading: false}
            });

            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        newOrderNextPageUrl: data.data.next_page_url,
                        cancelOrderList: data.data.data
                    }
                })
            }
        },
        //完成订单列表
        * finishOrderList({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            console.log('开始获取完成单列表', payload);
            const data = yield call(Request.get, Request.getApi().finishOrder, {store_id: auth.profile.id, ...payload})
            yield put({
                type: 'save',
                payload: {loading: false}
            });

            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        finishOrderNextPageUrl: data.data.next_page_url,
                        finishOrderList: data.data.data
                    }
                })
            }
        },
        //退款售后订单列表
        * refundOrderList({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            console.log('开始获取退款售后订单列表', payload);
            const data = yield call(Request.get, Request.getApi().refundOrder, {store_id: auth.profile.id, ...payload})
            yield put({
                type: 'save',
                payload: {loading: false}
            });

            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        refundOrderNextPageUrl: data.data.next_page_url,
                        refundOrderList: data.data.data
                    }
                })
            }
        },
        //进行中订单列表
        * progressOrderList({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });

            console.log('开始获取进行中订单列表', payload);
            const data = yield call(Request.get, Request.getApi().progressOrder, {store_id: auth.profile.id, ...payload})
            yield put({
                type: 'save',
                payload: {loading: false}
            });

            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        progressOrderNextPageUrl: data.data.next_page_url,
                        progressOrderList: data.data.data
                    }
                })
            }
        },

        //接受订单
        * acceptOrder({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });

            const data = yield call(Request.post, Request.getApi().changeNewOrderStatus, {
                store_id: auth.profile.id,
                issend: 2, ...payload
            })
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {

                yield put({type: 'order/fetchData'})
            }
        },

        //接受订单
        * rejectOrder({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });

            const data = yield call(Request.post, Request.getApi().changeNewOrderStatus, {
                store_id: auth.profile.id,
                issend: 8, ...payload
            });
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                yield put({type: 'order/fetchData'})
            }
        },
        //加载更多
        * loadMore({payload}, {call, put, select}) {

        }
    },
    subscriptions: {
        setup({dispatch}) {

        }
    },
};
